package com.zhangtai.utils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * AES加密解密
 * @author chengf
 *
 */
public class NeuAES {
	
	/**
     * 密码
     */
	private String pwd = null;
	
	/**
     * 构造器
     */
	public NeuAES() {
		
	}
	
	/**
     * 构造器
     * @param pwd 密码
     */
	public NeuAES(String pwd) {
		this.pwd = pwd;
	}
	
	/**
     * 将传入的明文转换为密文
     * 
     * @param str 明文字符串
     * @return 加密后的密文
     */
	public String encode(String str) {
		byte[] result = null;
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			if (pwd == null) {
				pwd = "ecology9";
			}
			random.setSeed(pwd.getBytes());
			kgen.init(128, random);
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			
			// 创建密码器
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] byteContent = str.getBytes();
			result = cipher.doFinal(byteContent);
			
		} catch (Exception e) {
			return null;
		}
		
		return parseByte2HexStr(result);
	}
	
	/**
     * 将传入的密文转换为明文
     * 
     * @param str 密文字符串
     * @return 解密后的明文
     */
	public String decode(String str) {
		byte[] result = null;
		byte[] content = parseHexStr2Byte(str);
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			if (pwd == null) {
				pwd = "ecology9";
			}
			random.setSeed(pwd.getBytes());
			kgen.init(128, random);
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			
			// 创建密码器
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, key);
			result = cipher.doFinal(content);
			
		} catch (Exception e) {
			return null;
		}
		
		return new String(result);
	}
	
	/**
     * 设置加密和解密的密钥
     * 
     * @param pwd 加密密钥
     * @return true：成功，false：失败
     */
	public boolean setPwd(String pwd) {
		this.pwd = pwd;
		return true;
	}
	
	/**
     * 设置加密和解密的向量
     * @param iv 加密向量
     * @return true：成功，false：失败
     */
	public boolean setIv(String iv) {
		return true;
	}
	
	/**
	 * 将二进制转换成十六进制
	 * 
	 * @param buf
	 * @return
	 */
	private String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}
	
	/**
	 * 将十六进制转换为二进制
	 * 
	 * @param hexStr
	 * @return
	 */
	private byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1) {
			return null;
		}
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}

	public static String  getToken(){
		com.zhangtai.utils.NeuAES neuAes = new com.zhangtai.utils.NeuAES();
		String content = "talentbase_"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		// AES的密钥长度最好是16位(不是必须)
		String password = "rort*94MttHat!1@";
		neuAes.setPwd(password);
		// 加密
		//System.out.println("加密前：" + content);
		String encodeResultStr = neuAes.encode(content);
//		System.out.println("加密后：" + encodeResultStr);
		return encodeResultStr;

	}
	
	/**
     * 主程序
     * @param args 参数
     */
	public static void main(String[] args) {
		com.zhangtai.utils.NeuAES neuAes = new com.zhangtai.utils.NeuAES();
		String content = "talentbase_"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		// AES的密钥长度最好是16位(不是必须)
		String password = "rort*94MttHat!1@";
		neuAes.setPwd(password);
		// 加密
		//System.out.println("加密前：" + content);
		String encodeResultStr = neuAes.encode(content);
		System.out.println("加密后：" + encodeResultStr);
		// 解密
		String decodeResultStr = neuAes.decode(encodeResultStr);
		System.out.println("解密后：" + decodeResultStr);
	}
	
}
